package jdbc.note8_5;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import jdbc.Config;

public class DBUtil {
	// 私有构造方法，禁止从外部创建此对象
	private DBUtil() {
	}

	static {
		try {
			Class.forName(Config.getClassdriver());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static Connection getCon() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(Config.getUrl(), Config.getUser(), Config.getPwd());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return con;
	}

	public static void close(ResultSet rs, Statement stm, Connection con) {
		close(rs);
		close(stm);
		close(con);
	}

	public static void close(ResultSet rs, PreparedStatement pstm, Connection con) {
		close(rs);
		close(pstm);
		close(con);
	}

	public static void close(Connection con) {
		if (con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close(PreparedStatement pstm) {
		if (pstm != null) {
			try {
				pstm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close(Statement stm) {
		if (stm != null) {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
